package com.nazca.tvremote.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.nazca.tvremote.R;
import com.nazca.tvremote.service.Controller;

public class TVRemoteService extends LinearLayout implements OnItemClickListener {

	private View mContainer;
	private GridView mGridView;
	private GridAdapter mAdapter;
	
	private static final int[] ICON = new int[]{
			R.drawable.ic_tv_remote_service_0, R.drawable.ic_tv_remote_service_1, R.drawable.ic_tv_remote_service_2,
			R.drawable.ic_tv_remote_service_3, R.drawable.ic_tv_remote_service_4, R.drawable.ic_tv_remote_service_5,
			R.drawable.ic_tv_remote_service_6, R.drawable.ic_tv_remote_service_7, R.drawable.ic_tv_remote_service_8
	};
	
	private static final int[] TITLE = new int[]{
			R.string.tv_remote_service_0, R.string.tv_remote_service_1, R.string.tv_remote_service_2,
			R.string.tv_remote_service_3, R.string.tv_remote_service_4, R.string.tv_remote_service_5,
			R.string.tv_remote_service_6, R.string.tv_remote_service_7, R.string.tv_remote_service_8
	};
	
	public TVRemoteService(Context context) {
		super(context);
	}
	public TVRemoteService(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		mContainer = findViewById(R.id.tv_remote_service);
		mGridView = (GridView) mContainer.findViewById(R.id.grid);
		mGridView.setOnItemClickListener(this);
		
		mAdapter = new GridAdapter();
		mGridView.setAdapter(mAdapter);
		
	}
	
	private class GridAdapter extends BaseAdapter{
    	
		@Override
		public int getCount() {
			return TITLE.length;
		}

		@Override
		public Object getItem(int position) {
			return TITLE[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView == null){
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, null);
				
				holder = new ViewHolder();
				holder.iconView = (ImageView) convertView.findViewById(R.id.icon);
				holder.titleView = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			holder.iconView.setImageResource(ICON[position]);
			holder.titleView.setText(TITLE[position]);
			
			return convertView;
		}
		
		private class ViewHolder{
			public ImageView iconView;
			public TextView titleView;
		}
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
	}
}
